package com.superman.service.impl;

import com.superman.dao.ICarDao;
import com.superman.dao.impl.CarDaoImpl;
import com.superman.entity.Car;
import com.superman.entity.Car_Brand_Category;
import com.superman.service.IAddAndAlter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddAndAlterImpl implements IAddAndAlter {
    ICarDao carDao = new CarDaoImpl();

    /**
     * 添加一条汽车记录
     * @param car 添加的汽车对象
     * @return 添加的汽车记录信息
     */
    @Override
    public List<Car_Brand_Category> addCar(Car car) {
        List<Car_Brand_Category> list = new ArrayList<>();
        try {
            int row = carDao.insertCar(car);
            list = carDao.queryCarByCarID(car.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 修改指定汽车 rent
     * @param car_id
     * @param rent
     * @return 修改后的汽车对象（以集合形式存放）
     */
    @Override
    public List<Car_Brand_Category> alterCarRent(String car_id, double rent) {
        List<Car_Brand_Category> list = new ArrayList<>();
        try {
            carDao.updateCarRent(car_id, rent);
            list = carDao.queryCarByCarID(car_id);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }

    /**
     * 修改指定汽车 useable
     * @param car_id
     * @param useable
     * @return 修改后的汽车对象
     */
    @Override
    public List<Car_Brand_Category> alterCarUseable(String car_id, int useable) {
        List<Car_Brand_Category> list = new ArrayList<>();
        try {
            carDao.updateCarUseable(car_id, useable);
            list = carDao.queryCarByCarID(car_id);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            return list;
        }
    }
}
